package cn.dw.mm.activiti;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.dw.mm.domain.Role;
import cn.dw.mm.domain.User;
import cn.dw.mm.domain.UserRole;
import cn.dw.mm.mapper.RoleMapper;
import cn.dw.mm.mapper.UserMapper;
import cn.dw.mm.mapper.UserRoleMapper;

/**
 * 自定义角色管理
 * 具体方法进入GroupEntityManager中查看
 */
@Component
public class CustomGroupEntityManager extends GroupEntityManager{
    @Autowired
    private UserMapper cusUserMapper;
    @Autowired
    private RoleMapper cusRoleMapper;
    @Autowired
    private UserRoleMapper cusUserRoleMapper;

    @Override
    public Group createNewGroup(String groupId) {
        return super.createNewGroup(groupId);
    }

    /**
     * 查找角色
     * @param userId
     * @return
     */
    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        System.out.println("userId:"+userId);
        User rocIdUser = cusUserMapper.selectByPrimaryKey(userId);
        List<UserRole> userRoleList = cusUserRoleMapper.selectByUserId(rocIdUser.getId());
        System.out.println("userRoleList size:"+userRoleList.size());
        List<Group> gs=new ArrayList<Group>();
        GroupEntity groupEntity;
        String roleId;
//        String activitiRole;
        for (UserRole userRole:userRoleList) {
            groupEntity=new GroupEntity();
            groupEntity.setRevision(1);
            groupEntity.setType("assignment");
            roleId=userRole.getRoleId();
            Role role=cusRoleMapper.selectByPrimaryKey(roleId);
            groupEntity.setId(role.getId());
            groupEntity.setName(role.getRoleName());
            gs.add(groupEntity);
        }
        return gs;
    }

}